var express = require("express");
var router = express.Router();
const db = require("../db/index.js");
const { v4: uuidv4 } = require('uuid');
// 新增驾校车辆

router.post("/addCar", function (req, res, next) {
  const info = req.body
  console.log(info);
  const sql = 'select * from car where plate=?'
  db.query(sql,info.plate,function(err,results){
    console.log(results);
    if(err) res.send({code:0,message:err.message})
    if(results.length>0) res.send({code:0,message:'该车已入库,请勿重复添加'})
    const sql1 = `insert into car set ?`
    let ID = uuidv4()
    ID = ID.replace(/-/g, '')
    db.query(sql1,{
      id:ID,
      plate:info.plate,
      model:info.model,
      color:info.color,
      img:info.img,
      status:'正常使用',
      coach:'暂未分配'
    },function(err,data){
      if(err) res.send({code:0,message:err.message})
      res.send({code:200,message:'添加成功！'})
    })
  })
   
});
// 删除车辆
router.post("/deleteCar", function (req, res, next) {
  let info = req.query;
  const sql = "delete from car where id=?";
  db.query(sql, info.id, function (err, results) {
    if (err) res.send({ message: err.message });
    res.send({ code: 200, message: "删除成功" });
  });
});
// 修改车辆状态
router.post("/update", function (req, res, next) {
    let info = req.body
  
  const sql =
    "update car set status=? where id=?";
  db.query(
    sql,
    [
      info.status,
      info.id
    ],
    function (err, results) {
      if (err) res.send({ message: err.message });
      if (results.affectedRows !== 1)
        res.send({ message: "出错了，请联系管理员" });
      res.send({
        code: 200,
        message: "修改车辆状态成功！",
      });
    }
  );
});
// 分配教练
router.post('/assignment',function(req,res,next){
  console.log(req.body);
  let info = req.body
  const sql =`update car set coach=? where id=?`
  db.query(sql,[info.coach,info.id],function(err,data){
    if (err) res.send({ message: err.message });
      if (data.affectedRows !== 1)
        res.send({ message: "出错了，请联系管理员" });
      res.send({
        code:200,
        message: "分配教练成功！",
      });
  })

})

// 查询车辆列表 分页

router.get('/list',function(req,res,next){
  console.log(req.query);
  // 获取参数   当前页码   当前每页数据量
  // 获取参数   当前页码   当前每页数据量
  let pageNumber = req.query.pageNumber
  // let pageSize = parseInt(req.body.pageSize)
  let pagesize = req.query.pagesize - 0
 // 求得sql语句中的参数
 console.log(pageNumber,pagesize);
//  if(!pageNumber) pageNumber=1
//  if(!pagesize) pagesize=6
 console.log(pageNumber,pagesize);
  let start = (pageNumber - 1) * pagesize
  // sql语句
  let sql = "select * from car limit ?, ?"
  let sqlParam = [start, pagesize]
  let sqlCount = "select count(*) from car"

  var count = 0
  // 执行查询
 db.query(sqlCount, null, (err, result) => {
   // 得到总记录数
   count = result[0]["count(*)"]
  
 })

 db.query(sql, sqlParam, (err, result) => {
   if (err) {
     res.send({
       code:0,
       message: err.message,
       ok:true
     })
   } else {
     res.send({
       code:200,
       message: "请求成功",
       ok: true,
       totalCount: count,
       data:result,
       
     })
   }
 })
   
})

// 查询车辆详情
router.get('/getInfo',function(req,res,next){
  console.log(req.query);
  let id = req.query.id
  const sql = `select * from car where id=?`
  db.query(sql,id,function(err,result){
    if(err) {return res.send({code:0,message:err.message})}else{
      return res.send({code:200,message:'查询成功！',data:result[0]})
    }
  })
   
})
// 模糊查询
router.post('/search',function(req,res,next){
  let plate = req.body.plate
  let  model = req.body.model
  let coach  = req.body.coach
  console.log(req.body);
  const sql =  `SELECT * FROM car where plate like '%${plate}%' or model like '%${model}%' or coach like '%${coach}%'`
  db.query(sql,function(err,results){
    if(err) {return res.send({code:0,message:err.message})} 
    res.send({code:200,message:'查询成功!',data:results})
  })
})

module.exports = router;
